package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocFileHistoryDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DocFileHistoryDtoServiceImpl implements DocFileHistoryDtoService {

    @Autowired
    private DocFileHistoryMapper docFileHistoryMapper;


    @Override
    public List<DocFileHistoryDto> queryHistory(Long fileId) {
        if (fileId == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        LambdaQueryWrapper<DocFileHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFileHistory::getFileId, fileId);
        List<DocFileHistory> docFileHistoryList = docFileHistoryMapper.selectList(wrapper);

        if (docFileHistoryList != null) {
            List<DocFileHistoryDto> docFileHistoryDtos = docFileHistoryList.stream().map(docFileHistory -> {
                DocFileHistoryDto docFileHistoryDto = new DocFileHistoryDto();
                docFileHistoryDto.setFileId(docFileHistory.getFileId());
                docFileHistoryDto.setFileName(docFileHistory.getFileName());
                docFileHistoryDto.setOperateUserId(docFileHistory.getOperateUserId());
                docFileHistoryDto.setOperateTime(new Date());
                docFileHistoryDto.setIsCreate((short) 1);
                docFileHistoryDto.setContent(docFileHistory.getContent());
                return docFileHistoryDto;
            }).collect(Collectors.toList());

            return docFileHistoryDtos;
        }
        return null;
    }
}
